<?php
//$_['id']=node_id
//$_['do']
//$_['data']={subnode|subtree}
//$_['access']={created|viewed|viewable}
//$_['output']={html|text}
//%escape		[t=title][d=description][c=creator][e=editor][p=parent_path]
//				[I=node_id][C=creator_id][T=create_time][M=modify_time][E=editor_id][V=pv]
//$_['format']

	include 'include/function.php';
	need_login();
	
function getParents($now_id) {
	$n=0;
	if($now_id<0)return '';
	while($now_id>=0) {
		$query="select parent,title,child_count from node where id=$now_id";
		$ret=oj_query($query);
		$row=mysql_fetch_row($ret);
		if(isset($row[0])) {
			$parent[$n][0]=$now_id;
			$parent[$n][1]=$row[1];
			$parent[$n][2]=$row[2];
			$now_id=$row[0];
		} else $now_id=-1;
		$n++;
	}
	$ret='';
	for($i=$n-1;$i>0;$i--) {
		$ret .= "{$parent[$i][1]}({$parent[$i][2]})&nbsp;&gt;&nbsp;";
	}
	return $ret . "{$parent[$i][1]}({$parent[$i][2]})";
}

function getRawData($id,$parents) {
	$row=mysql_fetch_assoc(oj_query('select title,description,create_id,modify_id,id,create_time,modify_time,visit_count,child_count '
		. " from node where id=$id"));
	if(!isset($row['id']))return array();
	return array(
		't'=>$row['title'],
		'd'=>$row['description'],
		'c'=>user_id_to_name($row['create_id']),
		'e'=>user_id_to_name($row['modify_id']),
		'p'=>(strlen($parents)?$parents . '&nbsp;&gt;&nbsp;':'') . "{$row['title']}({$row['child_count']})",
		'I'=>$row['id'],
		'C'=>$row['create_id'],
		'T'=>$row['create_time'],
		'M'=>$row['modify_time'],
		'E'=>$row['modify_id'],
		'V'=>$row['visit_count']
	);
}

function getData($format,$raw) {
	$s=explode('%%',$format);
	$n=count($s);
	for($i=0;$i<$n;$i++) {
		$sf=explode('%',$s[$i]);
		$nf=count($sf);
		for($j=1;$j<$nf;$j++) {
			$sf[$j]=$raw[$sf[$j][0]] . substr($sf[$j],1);
		}
		$s[$i]=implode($sf);
	}
	return implode('%',$s);
}

function dfs($id,$deep,$parents) {
	global $output_format,$_user_id,$export_access;
	$raw=getRawData($id,$parents);
	//echo 'test:' . getData($output_format,$raw);
	if($export_access=='created') {
		if($raw['C']==$_user_id)
			echo getData($output_format,$raw);
	} else if($export_access=='viewed') {
		$row=mysql_fetch_row(oj_query("select user_id from user_rate where user_id=$_user_id and node_id=$id limit 1"));
		if(isset($row[0]))
			echo getData($output_format,$raw);
	} else { //viewable
		$perm=permission_node($_user_id,$id);
		if(strpos($perm[0],'view')!==FALSE)
			echo getData($output_format,$raw);
	}
	if($deep==0)return;
	$ret=oj_query("select id from node where parent=$id");
	while($row=mysql_fetch_row($ret))
		dfs($row[0],$deep-1,$raw['p']);
}

	$output_format_html=<<<AAA
<div class="content">
	<div style="float:right;">
		<table><tbody>
			<tr><td>ID</td><td>%I</td></tr>
			<tr><td>PV</td><td>%V</td></tr>
			<tr><td>Create time</td><td>%T</td></tr>
			<tr><td>Edit time</td><td>%M</td></tr>
			<tr><td>Creator</td><td>%c</td></tr>
			<tr><td>Last edit</td><td>%e</td></tr>
		</tbody></table>
	</div>
	%p
	<center><p class="title">%t</p></center>
	<p>%d</p>
	<div style="clear:both;"></div>
</div>
<hr/>
AAA
;
	$output_format_text=<<<AAA
%t
%p
by %c at %T
%d
--------------------
AAA
;

	$export_id=get_to_int(get_request('id'));
	$export_do=get_request('do');
	
	$row=mysql_fetch_row(oj_query("select id,parent from node where id=$export_id"));
	if(!isset($row[0]))redirect_error(sprintf(_d('err_no_nodeD'),$export_id));
	if(empty($export_do)) {
		show_header(_d('title_export'));
		include_once 'include/form.php';
		
		draw_simple_form('post',"export.php?id=$export_id",array('do'=>'1','output'=>'html'),
		array(
			'id'=>array('html',_d('a_nid'),"<a href='shownode.php?id=$export_id'>$export_id</a>"),
			'data'=>array('option',_d('export_data'),'subtree','list'=>array('subnode'=>_d('export_subnode'),'subtree'=>_d('export_subtree'))),
			'access'=>array('option',_d('export_access'),'viewable','list'=>array('created'=>_d('export_created'),'viewed'=>_d('export_viewed'),'viewable'=>_d('export_viewable'))),
			'format'=>array('textarea',_d('export_format'),$output_format_html),
			'format_desc'=>array('html','',_d('export_format_desc')),
			'submit'=>array('submit','',_d('export_submit'))
		));
		
		include 'include/footer.php';
	} else {
		$export_output=get_request('output');
		$export_format=get_request('format');
		$export_access=get_request('access');
		$export_data=get_request('data');
		if($export_output=='html') {
			header('Content-type: text/html; charset=utf-8');
			header('Content-Disposition: attachment; filename="export.html"');
			echo '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body>';
			$output_format=$output_format_html;
		} else { //text
			header('Content-type: text/plain; charset=utf-8');
			header('Content-Disposition: attachment; filename="export.txt"');
			$output_format=$output_format_text;
		}
		if(!empty($export_format))$output_format=$export_format;
		//echo "output=$export_output,format=$export_format,access=$export_access,data=$export_data,id=$export_id";
		if($export_data=='subnode')
			dfs($export_id,1,getParents($row[1]));
		else
			dfs($export_id,-1,getParents($row[1]));
		
		if($export_output=='html')echo '</body></html>';
	}
	
?>


